//
//  WBStatusViewModel.swift
//  daoge_sineweibo
//
//  Created by 伍禄林 on 16/10/15.
//  Copyright © 2016年 伍禄林. All rights reserved.
//

import Foundation

//如果没有任何父类，要打印类的信息，需要遵守CustomStringConvertible 协议，实现description方法
class WBStatusViewModel: CustomStringConvertible {
    
    var status: WBStatusModel
    
    var memberIcon: UIImage?
    
    var vipIcon: UIImage?
    
    var repostsString: String?
    
    var commentsString: String?
    
    var attitudesString: String?
    
    var pictureSize = CGSize()
    
    //如果被转发的微博有图片就返回转发微博的图片。没有就返回原创微博的图片。都没有就返回空
    var picUrls:[WBPictureModel]? {
        
        return status.retweeted_status?.pic_urls ?? status.pic_urls
    }
    
    var retweetedStatusText: String?
    
    init(model: WBStatusModel) {
        self.status = model
        
        if (status.user?.mbrank)! > 3 && (status.user?.mbrank)! < 7 {
            
            memberIcon = UIImage(named: "navigationbar_friendsearch_highlighted")
        }
        
        switch status.user?.verified_type ?? -1 {
        case 0:
            vipIcon = UIImage(named: "main_badge") //vip
        case 2,3,5:
             vipIcon = UIImage(named: "main_badge") //企业认证
        case 220:
            vipIcon = UIImage(named: "main_badge") //达人
        default: break
            
        }
        
        repostsString = countString(count: status.reposts_count,defaultStr: "转发 ")
        commentsString =  countString(count: status.comments_count ,defaultStr: "评论 ")
        attitudesString = countString(count: status.attitudes_count, defaultStr: "赞 ")
        
        pictureSize = calePicViewSize(count: picUrls?.count)
        
        retweetedStatusText = "@" + (status.retweeted_status?.user?.screen_name ?? "") + ":" + (status.retweeted_status?.text ?? "")
        
    }
    
    
    
    var description: String {
        return status.description
    }
    
    //计算图片视图的宽高
    func calePicViewSize(count:Int?) -> CGSize {
        
        if count == 0 || count == nil{
            return CGSize()
        }
        
        let row = (count! - 1) / 3 + 1
        
        let WBStatusPictureViewHeight = WBStatusPictureViewOutMargin  + CGFloat(row) * WBStatiusPitureItemWidth  + CGFloat(row -  1) * WBStatusPictureViewInMargin
        
        return CGSize(width: WBStatiusPitureItemWidth, height: WBStatusPictureViewHeight)
        
    }
    
    //更新单张图片的视图尺寸
    func updataSinglePicSize(image: UIImage)  {
        var size = image.size
        
        let maxWith = CGFloat(300)
        let minWith = CGFloat(40)
        //图片过宽处理
        if size.width > maxWith {
            
            size.width = maxWith
            
            size.height = size.width * image.size.height / image.size.width
        }
        //图片过窄处理
        if size.width < minWith {
            
            size.width = minWith
            
            size.height = size.width * image.size.height / image.size.width / 4
        }
        size.height += WBStatusPictureViewOutMargin
        pictureSize = size
    }
    
    func countString(count: Int, defaultStr: String) -> String {
        
        if count == 0 {
            return defaultStr
        }
        
        if count < 10000 {
            return defaultStr + count.description
        }
        
        return String(format: "%0.2f 万", Double(count) / 10000)
        
    }
    
    
}
